package com.itheima.bakery.demos.web.Repositories;

import com.itheima.bakery.demos.web.model.Inventory;
import com.itheima.bakery.demos.web.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
    @Modifying
    @Transactional
    @Query(value = "UPDATE products SET stock_quantity = stock_quantity - :quantity WHERE product_id = :productId", nativeQuery = true)
    void decrementStock(@Param("productId") Integer productId, @Param("quantity") int quantity);

    Inventory findInventoryById(Integer productId);

    List<Product> findByNameContainingIgnoreCase(String name);
    List<Product> searchProductsByDescriptionContainingIgnoreCase(String query);
    List<Product> findByNameContaining(String keyword);

}
